package com.imooc.distributedemo.resume.arith;

import com.alibaba.druid.util.StringUtils;

import java.util.Stack;

/**
 * 反转字符串的单次
 */
public class RevertStringLetterSolution {

    public static String reverseWords(String s) {
        /*if(StringUtils.isEmpty(s)){
            return s;
        }
        String tmpStr = s.trim();
        String[] tmpStrArr = tmpStr.trim().split("\\s+");
        StringBuilder ssb = new StringBuilder("");
        for (int i = tmpStrArr.length-1; i >=0 ; i--) {
            ssb.append(tmpStrArr[i]);
            ssb.append(" ");
        }
        return ssb.toString().trim();*/
        boolean notEmptyData = null != s && !"".equals(s);
        if(!notEmptyData){
            return s;
        }
        String tmpStr = s.trim();
        String[] tmpStrArr = tmpStr.trim().split("\\s+");
        StringBuilder ssb = new StringBuilder("");
        for (int i = tmpStrArr.length-1; i >=0 ; i--) {
            ssb.append(tmpStrArr[i]);
            ssb.append(" ");
        }
        return ssb.toString().trim();
    }

    public static String reverseWordsUseStack(String s) {
        boolean notEmptyData = null != s && !"".equals(s);
        if(!notEmptyData){
            return s;
        }
        String tmpStr = s.trim();
        String[] tmpStrArr = tmpStr.trim().split("\\s+");
        Stack<String> stack = new Stack<>();
        for (int i = 0; i < tmpStrArr.length; i++) {
            stack.push(tmpStrArr[i]);
        }
        String result = "";
        while (!stack.isEmpty()){
            result += stack.pop();
            result += " ";
        }
        return result.trim();
    }

    public static void main(String[] args) {
        String data = "lily is a beautiful girl ";
        String result = reverseWordsUseStack(data);
        System.out.println(result);



        /*Stack<String> stackData = new Stack<>();
        stackData.push("abc");
        stackData.push("123");
        stackData.push("###");
        stackData.push("---");

        System.out.println(stackData.pop());
        System.out.println(stackData.pop());
        System.out.println(stackData.pop());
        System.out.println(stackData.pop());*/
        // result should be "girl beautiful a is lily"
    }
}
